package com.cenntro.repair.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.curator.shaded.com.google.common.collect.ImmutableMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cenntro.common.base.BaseController;
import com.cenntro.common.model.vo.PageVO;
import com.cenntro.common.util.result.PageResult;
import com.cenntro.common.util.result.Result;
import com.cenntro.repair.model.dbo.SysMessageDO;
import com.cenntro.repair.service.SysMessageService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @ClassName: SysMessageController
 * @Description: 维修消息
 * @author Administrator
 * @date 2019年7月10日
 */
@Api(value = "维修消息", tags = { "维修消息" })
@RestController
@RequestMapping("/repairMessage")
@Slf4j
public class SysMessageController extends BaseController {

    @Resource
    private SysMessageService sysMessageService;

    /**
     * 
     * @Title list 
     * @Description 消息列表 
     * @param pageVO 
     * @return list 
     * @throws
     */
    @RequestMapping("/list")
    public Result list(PageVO pageVO, Integer sooid) {
        Page<SysMessageDO> page = PageHelper.startPage(pageVO.getPage(), pageVO.getLimit(), true);
        List<SysMessageDO> list = sysMessageService.selectPage(ImmutableMap.of("userId", sooid));
        return PageResult.ok(list, page.getTotal());
    }

    /**
     * 
     * @Title change 
     * @Description 消息变更 
     * @param messageId
     * @return SysMessageDO
     * @throws
     */
    @CrossOrigin
    @RequestMapping("/change")
    public Result change(Integer messageId, Integer sooid) {
        if (messageId != null && sooid != null) {
            SysMessageDO messageDO = sysMessageService
                    .selectOne(ImmutableMap.of("messageId", messageId, "userId", sooid));
            if (messageDO != null) {
                messageDO.setStatus(1);
                sysMessageService.update(messageDO);
                return Result.ok();
            }
        } 
        return Result.error("不存在");
    }

    /**
     * 
    * @Title delete
    * @Description 消息删除(物理删除)
    * @param messageId
    * @return  
    * @throws
     */
    @RequestMapping("/delete")
    public Result delete(Integer messageId, Integer sooid) {
        if (messageId != null && sooid != null) {
            SysMessageDO messageDO = sysMessageService
                    .selectOne(ImmutableMap.of("messageId", messageId, "userId", sooid));
            if (messageDO != null) {
                messageDO.setStatus(-1);
                sysMessageService.update(messageDO);
                return Result.ok();
            }
        }
        return Result.error("不存在");
    }
}
